﻿@{ 
    Layout = "~/Views/Shared/_ModalForm.cshtml";
    string defId = ViewBag.DefaultId;
}

<div class="wrapper animated fadeInRight">
    <form id="form" class="form-horizontal m">
        <div class="form-group">
            <label class="col-sm-3 control-label">类别</label>
            <div class="col-sm-8 " id="type" col="Type"></div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">上级组织</label>
            <div class="col-sm-8">
                <div id="parentId" col="ParentId"></div>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label ">名称<font class="red"> *</font></label>
            <div class="col-sm-8">
                <input id="name" col="Name" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">负责人</label>
            <div class="col-sm-8">
                <div id="owner" col="Owner"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">电话号码</label>
            <div class="col-sm-8">
                <input id="telephone" col="Telephone" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">传真</label>
            <div class="col-sm-8">
                <input id="fax" col="Fax" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">Email</label>
            <div class="col-sm-8">
                <input id="email" col="Email" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">排序码</label>
            <div class="col-sm-8">
                <input id="sequence" col="Sequence" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">状态</label>
            <div class="col-sm-8" id="status" col="Status"></div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">描述</label>
            <div class="col-sm-8">
                <textarea id="description" col="Description" class="form-control" style="height:68px"></textarea>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    var id = ys.request("id");
    var Top = ys.request("addTop");
    var parentId = ys.request("parentId");
    var parentName = ys.request("parentName");
    var act = 'edit';
    if (id == '' | id == null) act = 'add';
    var defType =0;

    $(function () {
        $("#type").ysComboBox({
            data: parent.getConfigDetailsByMasterId('@((int)FuncConfigSubType.AdminOrgType)'),
            key: "Id",
            value: "Name",
            class: "no-additional-option"
        });
        if (act == 'add') {
            defType = parent.getConfigDetailDefaultIdByMasterId('@((int)FuncConfigSubType.AdminOrgType)');
        }

        $("#status").ysRadioBox({ data: ys.getJson(@Html.Raw(typeof(StatusType).EnumToDictionaryJson())) });
        $('#owner').ysComboBoxTree({ url: '@Url.Content("~/Sys/Organization/GetUserTreeJsonForSelectOwner")', async: false });
        $('#parentId').ysComboBoxTree({ url: '@Url.Content("~/Sys/Organization/GetOrganizationTreeJsonForSelectParent")', async: false });

        if (parentId == '@defId') {
            $('#parentId_input').attr('disabled', 'true');
            $("#parentId_input").val("根目录");
        }

        getForm();

        $("#form").validate({
            rules: {
                name: { required: true, maxlength: 31  },
                telephone: {maxlength: 31 },
                email: {maxlength: 31 },
                fax: {maxlength: 31 },
            }
        });
    });

    function getForm() {
        if (act =='add') {
            ys.ajax({
                 url: '@Url.Content("~/Sys/Organization/GetMaxSequenceNoJson")' + '?parentId=' + parentId,
                type: "get",
                success: function (rst) {
                    if (rst.Flag == 1) {
                        var defaultData = {};
                        defaultData.ParentId = parentId;
                        defaultData.Sequence = rst.Data;
                        defaultData.Type = defType;
                        defaultData.Status = 1;
                        $("#owner_input").val("请选择负责人");
                        $("#type_input").val("请选择类别");
                        $("#form").setWebControls(defaultData);
                    }
                    else ys.msgError(rst.Message);

                }
             });
        }
        else {//edit
            ys.ajax({
                url: '@Url.Content("~/Sys/Organization/GetEditDtoJson")' + '?id=' + id,
                type: "get",
                success: function (rst) {
                    if (rst.Flag == 1) {
                        var result = rst.Data;
                        $("#form").setWebControls(result);
                    }
                    else ys.msgError(rst.Message);
                }
            });
        }
    }

    function saveForm(index) {
        if (!ys.checkTextLenth("description", "描述", 0, 127)) return;
        if (!ys.checkComboboxSelection("type_select", "类别")) return;

        if ($("#form").validate().form()) {
            var postData = $("#form").getWebControls({ Id: id });
            postData.ParentId = ys.getLastValue(postData.ParentId);
            postData.Owner = ys.getLastValue(postData.Owner);
            ys.ajax({
                url: '@Url.Content("~/Sys/Organization/")' + act,
                type: "post",
                data: postData,
                success: function (rst) {
                    if (rst.Flag== 1) {
                        ys.msgSuccess(rst.Message);
                        parent.searchTreeGrid(postData.ParentId);
                        parent.layer.close(index);
                    }
                    else {
                        ys.msgError(rst.Message);
                    }
                }
            });
        }
    }
</script>
